package com.mhy.pickerview;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
//import com.zhy.autolayout.utils.AutoUtils;
//
//import butterknife.ButterKnife;
//import butterknife.Unbinder;

/**
 * @author Wudi
 * @date 2019/4/16
 */
public abstract class BaseDialogFragment extends DialogFragment {
    protected String TAG = this.getClass().getSimpleName();
    private static final String SAVED_DIALOG_STATE_TAG = "android:savedDialogState";
//    Unbinder unbinder;

//    @Override
//    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
//        super.onActivityCreated(savedInstanceState);
//        if (getDialog() != null) {
//            try {
//                // 解决Dialog内D存泄漏
//                //根本不能解决问题，在super.onActivityCreated(savedInstanceState)中
//                // 仍然会handler先持有DialogFragment,
//                getDialog().setOnDismissListener(null);
//                getDialog().setOnCancelListener(null);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }

//Dialog内D存泄漏最终解决方案，重写onActivityCreated方法，
//并在super.onActivityCreated(savedInstanceState);修改dialog显示
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        if (getShowsDialog()) {
            setShowsDialog(false);
        }
        super.onActivityCreated(savedInstanceState);
        setShowsDialog(true);

        View view = getView();
        if (view != null) {
            if (view.getParent() != null) {
                throw new IllegalStateException(
                        "DialogFragment can not be attached to a container view");
            }
            getDialog().setContentView(view);
        }
        final Activity activity = getActivity();
        if (activity != null) {
            getDialog().setOwnerActivity(activity);
        }
        if (savedInstanceState != null) {
            Bundle dialogState = savedInstanceState.getBundle(SAVED_DIALOG_STATE_TAG);
            if (dialogState != null) {
                getDialog().onRestoreInstanceState(dialogState);
            }
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        // 全屏显示Dialog，重新测绘宽高
        if (getDialog() != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(getLayoutResId(), container, false);
//        AutoUtils.auto(rootView);
//        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    /**
     * 获取布局文件
     *
     * @return
     */
    protected abstract int getLayoutResId();

    /**
     * 初始化界面
     */
    protected void initView() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        unbinder.unbind();
    }


    /**
     * 全屏显示Dialog
     *
     * @param savedInstanceState
     * @return
     */
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        return dialog;
    }

}